package com.heima.rabbitmq.consumer.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @program: springbootRabbitmq->MyListener
 * @description: 消费者
 * @author: G_Y
 * @create: 2019-08-11 09:45
 **/
@Component
@RabbitListener(queues = "item_queue",containerFactory = "customContainerFactory")
public class MyListener {
//    @RabbitListener(queues = "item_queue")
////    @RabbitHandler
//    public void myListener1(String message) {
//        System.out.println("消费者接收到的消息为: " + message);
////        throw new RuntimeException("消息消费异常");
//    }

    @RabbitHandler
    public void messageListen(String receptionString, Message message, Channel channel) throws IOException {
        byte[] body = message.getBody();
        String s = new String(body, "utf-8");
        System.out.println(s);
        System.out.println("------------");
        System.out.println(receptionString);
        try {
            // 消费业务逻辑
//            throw new RuntimeException("业务处理异常啦");
            // 手动确认消费成功——移除消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            // 拒绝
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
            // 未确认
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
        }
    }

//    public static void main(String[] args) {
//        Integer i = 127;
//        Integer i2 = 127;
//        System.out.println(i == i2);
//    }
}
